﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Abp.Dependency;
using Abp.EntityFrameworkCore.Configuration;
using Abp.Modules;
using Abp.MultiTenancy;
using Abp.Reflection.Extensions;
using Castle.MicroKernel.Registration;
using GCT.Foundation.Configuration;
using GCT.Foundation.Database;
using GCT.Foundation.Databases;
using GCT.Foundation.EntityFrameworkCore;
using GCT.Foundation.Seed;
using L._52Abp;
using L._52Abp.Database;

namespace GCT.Foundation
{
    [DependsOn(
        typeof(GCTSharedEntityFrameworkCoreModule),
        typeof(FoundationTemplateCoreModule)
    )]
    public class FoundationTemplateEntityFrameworkCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            IocManager.RegisterIfNot(
                typeof(ISqlExecutor),
                typeof(FoundationTemplateSqlExecutor),
                DependencyLifeStyle.Transient
            );

            // 注册dbcontext
            if (!FoundationTemplateConfigs.Database.SkipDbContextRegistration)
            {
                Configuration.Modules.AbpEfCore()
                    // Configuration.Modules.L52AbpEfCore()
                    .AddDbContext<FoundationTemplateDbContext>(options =>
                    {
                        if (options.ExistingConnection != null)
                        {
                            FoundationTemplateDbContextConfigurer.Configure(
                                options.DbContextOptions,
                                options.ExistingConnection
                            );
                        }
                        else
                        {
                            FoundationTemplateDbContextConfigurer.Configure(
                                options.DbContextOptions,
                                options.ConnectionString
                            );
                        }
                    });
            }
        }

        public override void Initialize()
        {
            // 注册程序集中的依赖
            IocManager.RegisterAssemblyByConvention(this.GetType().GetAssembly());

            // 注册数据库检查器
            IocManager.Register(
                typeof(IFoundationTemplateDatabaseChecker),
                typeof(DatabaseChecker<FoundationTemplateDbContext>),
                DependencyLifeStyle.Transient
            );

            // 使用template的迁移器实现
            if (IocManager.IsRegistered<IAbpZeroDbMigrator>())
            {
                IocManager.IocContainer.Register(
                    Component.For<IAbpZeroDbMigrator>()
                        .UsingFactoryMethod((k)=>k.Resolve<FoundationTemplateDbMigrator>())
                        .LifestyleTransient()
                        .IsDefault()
                );
            }
        }


        public override void PostInitialize()
        {
            // 创建种子数据
            var configurationAccessor = IocManager.Resolve<IAppConfigurationAccessor>();
            using var scope = IocManager.CreateScope();
            var connStr = configurationAccessor.Configuration.ConnectionStringsDefault();
            var dbExist = scope.Resolve<IDatabaseChecker<FoundationTemplateDbContext>>()
                .Exist(connStr);

            if (!FoundationTemplateConfigs.Database.SkipDbSeed && dbExist)
            {
                FoundationTemplateSeedHelper.SeedHostDb(IocManager);
            }
        }
    }
}
